<template>
  <div class="login-page">
    <ValidateForm @form-submit="onFormSubmit">
      <div class="mb-3">
        <label for="exampleFormControlInput1" class="form-label">邮箱地址</label>
        <validate-input
        placeholder="请输入正确的电子邮箱信息"
        v-model="emailVal"
        :rules="emailRules"
        @value="emailValue"
        ></validate-input>
      </div>
      <div class="mb-3">
        <label for="exampleFormControlTextarea1" class="form-label">密码</label>
        <validate-input
          placeholder="请输入密码"
          v-model="passwordVal"
          :rules="passwordRules"
          @value="passwordValue"></validate-input>
      </div>
      <template #submit>
        <span class="btn btn-danger">登录</span>
      </template>
    </ValidateForm>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { useStore } from 'vuex'
import ValidateForm from '@/components/ValidateForm.vue'
import ValidateInput, { RulesProp } from '@/components/ValidateInput.vue'
import { useRouter } from 'vue-router';

export default defineComponent({
  name: "LoginIndex",
  //components的意思：把这个组件引进来
  components: {
    ValidateForm,
    ValidateInput
  },

  setup() {
    const emailVal = ref('')
    const emailRules: RulesProp = [
      //这个是邮箱规则，应该被这个东西所约束
      { type: 'required', message:'电子邮箱地址不能为空'},
      { type: 'email', message:'请输入正确的电子邮箱格式'}
    ]
    const passwordVal = ref('')
    const passwordRules: RulesProp=[
      { type: 'required', message: '密码不能为空'}
    ]
    const emailValue = (val: string) => {
      emailVal.value = val
    }
    const passwordValue = (val: string) => {
      passwordVal.value = val
    }
     const store = useStore()
    const router = useRouter()
    const onFormSubmit = (result: boolean) => {
      console.log('123', result)
      if(result) {
        //发送登录
        const payload = {
          email: emailVal.value,
          password: passwordVal.value
        }
        store.dispatch('loginAndFecth', payload).then((data) => {
          //发送登录
          router.push('/')
          
        })
      }
      
    }
 
    return {
      //数据如何在组件中使用：
      // 1.将数据抛出
      //2.将组件写在结构里
      emailRules,
      emailVal,
      emailValue,
      onFormSubmit,
      passwordVal,
      passwordRules,
      passwordValue
      // validateEmail
    }
  }
})
</script>

<style scoped></style>
